// miniprogram/pages/aifont/myspread/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datas: [],
    dataSize: -1,
    name: "",
    money: "",
    amount: "",
    miniMoney: "-",
    fee: "-"
  },
  cashType:"work_cash",

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var params = {};
    params.key = this.cashType;
    app.postRequestLoading("/rest/cashconfig/config.htm", params, this.backConfigData);

  },
  backConfigData(res) {
    if (res.code == 0) {
      this.setData({
        amount: res.money,
        fee: res.fee,
        miniMoney: res.miniMoney,
        feeType:res.feeType,
        rate:res.rate
      });
    }
  },
  no: 1,
  init: function () {
    this.no = 1;
    var params = {};
    params.no = this.no;
    params.size = 10;
    app.postRequestLoading("/rest/cashpayment/my.htm", params, this.backData);
  },
  loadMore: function () {
    this.no = this.no + 1;
    var params = {};
    params.no = this.no;
    params.size = 10;
    app.postRequestLoading("/rest/cashpayment/my.htm", params, this.backDataMore);
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  backData: function (res) {
    console.info(res);
    if (res.code == 0) {
      this.totalPage = res.totalPage;
      this.setData({
        datas: res.list,
        dataSize: res.total
      });
    }
  },
  backDataMore: function (res) {
    console.info(res);
    if (res.code == 0) {
      this.totalPage = res.totalPage;
      var dataList = this.data.datas.concat(res.list);
      this.setData({
        datas: dataList,
        dataSize: res.total
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.info("下拉");
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.info("上拉");
    if (this.no < this.totalPage) {
      this.loadMore();
    }
  },
  cash() {
    var params = {};

    params.name = this.data.name;
    params.money = this.data.money;
    params.openId = app.openId;
    params.key = this.cashType;
    if (this.checkNull(params.money)) {
      app.showModal("请填写提现金额");
      return;
    }
    var cashMoney = this.parseMoney(params.money);
    var miniMoney = this.parseMoney(this.data.miniMoney);
    if(cashMoney<miniMoney){
      app.showModal("提现金额小于最小提现金额");
      return;
    }

    if (this.checkNull(params.name)) {
      app.showModal("请填写真实姓名");
      return;
    }
    app.postRequestLoading("/rest/cashpayment/cashAuto.htm", params, this.backDataCash);

  },
  checkNull(str) {
    if (str == undefined) {
      return true;
    }
    if (str == null) {
      return true;
    }
    if (str == "") {
      return true;
    }
    return false;
  },
  parseMoney(str) {
    var money = 0;
    try {
      money = parseFloat(str);
    } catch (error) {

    }
    return money;
  },
  backDataCash(res) {
    
    if (res.code == 0) {
      this.setData({money:"",name:""});
      app.showModal(res.msg,()=>{
        wx.navigateBack({
          delta: 0,
        })
      });
     
    } else {
      app.showModal(res.msg);
    }
  }
})